<?php
namespace common\models;


class StaticInstance extends BaseModel
{


    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'static_instance';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id', 'model_id', 'mch_id', 'type', 'bonus_day', 'create_time', 'update_time','commodity_id','order_type'], 'integer'],
            [['in_num', 'total_num', 'remain_num', 'was_num', 'bonus_num','period_day'], 'number'],
            [['status'], 'string', 'max' => 1],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => 'Member ID',
            'model_id' => '对应的模式id',
            'mch_id' => 'Mch ID',
            'in_num' => '投资总金额',
            'total_num' => '总数量 放大后的',
            'remain_num' => '剩余总数',
            'was_num' => '已释放数量',
            'bonus_num' => '每天释放数量 有这个值对以这个值释放',
            'type' => '1自己投资的 2动态的',
            'status' => '状态1 正常 2完成',
            'bonus_day' => '分红天数',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
	{
		$model  = self::find();
		$where  = isset($whereArr['where']) ? $whereArr['where'] : [];
		$whereAnd = isset($whereArr['and'])?$whereArr['and']:[];

		/*默认全部*/if (isset($where['type'])) {
        if ($where['type'] == 1) {
            unset($where['type']);
            $where = ['member_id' => $params['member_id'],'order_type'=>['1','2','3']];
            /*收益中*/
        } elseif ($where['type'] == 2) {
            $where = ['member_id' => $params['member_id'], 'status' => 1, 'order_type' => [2, 3]];
            /*已到期*/
        } elseif ($where['type'] == 3) {
            $where = ['member_id' => $params['member_id'], 'status' => 2,'order_type' => [2, 3]];
            /*自由*/
        } elseif ($where['type'] == 4) {
            $where = ['member_id' => $params['member_id'], 'status' => 1, 'order_type' => '1'];
        }

    }

		$models = self::queryFormart($model,$where,$params,$whereAnd);
		$model  = $models['model'];
		$data  = $model->asarray()->all();

		if(!$data) return array();

		//扩展信息
//		if(!$extends) return $data;
        $times=[];
        foreach ($data as $k => $v) {
            $data[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);


            foreach($extends as $extend) {
		    if ($extend=='winning') {
                $i = 1;
                $j = 1;
                    $dayIncome = TradeCommodity::getOne(['commodity_id' => $v['commodity_id']], 'day_income');
                    $data[$k]['day_income'] = $dayIncome['day_income'] ?? 0;
                    if ($v['order_type'] <> 1) {
                        $data[$k]['name'] = '合约理财NO.' . $j;
                        $j++;
                        if ($v['status'] == 1) {
                            /*收益*/
                            $data[$k]['category'] = 2;
                        } else {
                            /*过期*/
                            $data[$k]['category'] = 3;
                        }
                        $data[$k]['overdue_time'] = date('Y-m-d', strtotime('+ ' . $v['period_day'] . ' day', $v['create_time']));

                        $data[$k]['overdue'] = $v['period_day'] - $v['bonus_day'];
                    } else {

                        $data[$k]['name'] = '自由理财NO.' . $i;
                        $i++;
                        if ($v['status'] == 1) {
                            /*自由*/
                            $data[$k]['category'] = 4;
                        } else {
                            /*过期*/
                            $data[$k]['category'] = 3;
                        }
                        $data[$k]['overdue_time'] = '';
                        $data[$k]['overdue'] = 0;
                    }

            }
            }
        }


		return $data;	
	}

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}
    /*
        * 投资列表
        * whereArr 条件
        * params 基本参数 包含 field order page limit
        * extends  扩展信息 一些相关的信息
        *
    */
    public static function investList($whereArr,$params,$extends=array())
    {
        $model  = self::find();
        $where  = $whereArr['where'];
        $whereAnd = isset($whereArr['and'])?$whereArr['and']:[];
        $models = self::queryFormart($model,$where,$params,$whereAnd);
        $model  = $models['model'];
        self::$pages = $models['pages'];

        $data  = $model->asarray()->all();
        if(!$data) return array();
        foreach ($data as $k=>$v){
            $data[$k]['create_time']= date('Y-m-d H:i:s',$v['create_time']);
        }

        //扩展信息
        if(!$extends) return $data;
        return $data;
    }

}